package com.bedpotato.thread;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.bedpotato.constants.ProxyClawerConstants;
import com.bedpotato.http.BasicHttpMethod;
import com.bedpotato.po.ProxyPO;
import com.bedpotato.service.ProxyClawerService;

@Component
public class CnproxySiteClawerThread implements Runnable {
	Logger logger = Logger.getLogger(CnproxySiteClawerThread.class);
	@Resource
	ProxyClawerService proxyClawerService;

	public static void main(String[] args) {
		CnproxySiteClawerThread cnproxySiteClawerThread = ProxyClawerConstants.applicationContext.getBean("cnproxySiteClawerThread",CnproxySiteClawerThread.class);
		cnproxySiteClawerThread.run();
	}

	@Override
	public void run() {
		logger.info("Cnproxy信息抓取启动");
		Set<ProxyPO> proxySet = getCnproxyProxyData();
		proxyClawerService.saveProxy(proxySet);
		logger.info("Cnproxy信息抓取结束");
	}

	public Set<ProxyPO> getCnproxyProxyData() {
		BasicHttpMethod httpMethod = new BasicHttpMethod();
		Set<ProxyPO> proxySet = new HashSet<ProxyPO>();
		for (int i = 1; i <= 5; i++) {
			try {
				String pageStr = httpMethod.get("http://www.cnproxy.com/proxy"
						+ i + ".html");
				String keywordPageStr = StringUtils.substringBetween(pageStr,
						"Country/Area</td></tr>", "</table>").trim();
				String[] contentStrArray = keywordPageStr.split("</tr>");
				for (String contentStr : contentStrArray) {
					String[] keywordStrArray = StringUtils.substringsBetween(
							contentStr, "<td>", "</td>");
					ProxyPO proxyPO = new ProxyPO();
					proxyPO.setIp(StringUtils.substringBetween(
							keywordStrArray[0], "", "<SCRIPT"));
					proxyPO.setPort(cnproxyPortConvert(keywordStrArray[0]
							.substring(keywordStrArray[0].indexOf("\":\"") + 4,
									keywordStrArray[0].indexOf(")<"))));
					// System.out.println(proxyPO.getIp() + ":"
					// + proxyPO.getPort());
					proxySet.add(proxyPO);
				}
			} catch (Exception e) {
				logger.error("Cnproxy " + e);
			}
		}
		return proxySet;
	}

	/**
	 * 把JS代码中加密的Port转换为数字
	 * 
	 * @author LiALuN
	 * @param jsPortStr
	 * @return
	 */
	private String cnproxyPortConvert(String jsPortStr) {
		Map<String, String> jsPortRelationMap = new HashMap<String, String>();
		jsPortRelationMap.put("z", "3");
		jsPortRelationMap.put("m", "4");
		jsPortRelationMap.put("k", "2");
		jsPortRelationMap.put("l", "9");
		jsPortRelationMap.put("d", "0");
		jsPortRelationMap.put("b", "5");
		jsPortRelationMap.put("i", "7");
		jsPortRelationMap.put("w", "6");
		jsPortRelationMap.put("r", "8");
		jsPortRelationMap.put("c", "1");
		String[] portStrArrays = jsPortStr.split("[+]");
		String port = new String();
		for (String portStr : portStrArrays) {
			port += jsPortRelationMap.get(portStr);
		}
		return port;
	}
}
